﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;

namespace Rose.Helper
{
    public class FeelingCommentHelpModel
    {
        private  Database_RoseEntities db = new Database_RoseEntities();

 

        #region  实现类型转换
        
        public  CommentModel ChangeCommentToCommentModel(FeelingComment comment)
        {
            string userId = comment.UserId;
            UserInfo userInfo = new CheckHelpModel().CheckUserInfo(userId);
            return new CommentModel()
            {
                ImagePath = userInfo.ImagePath,
                Content = HttpContext.Current.Server.HtmlEncode(comment.Content),
                CreateTime = comment.CreateTime.ToString(),
                Id = comment.Id.ToString(),
                UserId = userId,
                Name=userInfo.Name
            };
        }

        private  List<CommentModel> ChangeCommentsToCommentModels(List<FeelingComment> comments)
        {
            List<CommentModel> returnModel = new List<CommentModel>();
            foreach (var comment in comments)
            {
                returnModel.Add(ChangeCommentToCommentModel(comment));
            }
            return returnModel;
        }
        #endregion

        #region  或者首个 最后一个
        private  CommentModel GetFirstCommentModel(Guid feelingId)
        {
            return ChangeCommentToCommentModel(db.FeelingComments.Where(c => c.FeelingId == feelingId).OrderBy(c => c.CreateTime).First());
        }

        private  CommentModel GetLastCommentModel(Guid feelingId)
        {
            return ChangeCommentToCommentModel(db.FeelingComments.Where(c => c.FeelingId == feelingId).OrderByDescending(c => c.CreateTime).First());
        }

        public  CommentModel GetFirstCommentModel(string feelingId)
        {
            return GetFirstCommentModel(new Guid(feelingId));
        }

        public  CommentModel GetLastCommentModel(string feelingId)
        {
            return GetLastCommentModel(new Guid(feelingId));
        }
        #endregion

        #region 获取列表

      

        #region feelingComment
        private  List<FeelingComment> GetFeelingCommentList(Guid feelingId, bool isDescending)
        {
            if (isDescending)
                return db.FeelingComments.Where(c => c.FeelingId == feelingId).OrderByDescending(c => c.CreateTime).ToList();
            else
                return db.FeelingComments.Where(c => c.FeelingId == feelingId).OrderBy(c => c.CreateTime).ToList();

        }

        private  List<FeelingComment> GetFeelingCommentList(string feelingId, bool isDescending)
        {
            return GetFeelingCommentList(new Guid(feelingId), isDescending);
        }


        public   List<CommentModel> GetCommentModelList(string feelingId, bool isDescending)
        {
            return ChangeCommentsToCommentModels(GetFeelingCommentList(feelingId, isDescending));
        }


        #endregion
        //private  int GetCommentCount(Guid feelingId)
        //{
        //    return db.FeelingComments.Where(c => c.FeelingId == feelingId).Count();
        //}

        //public  int GetCommentCount(string feelingId)
        //{
        //    return GetCommentCount(new Guid(feelingId));
        //}

        #endregion





    }
}